package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.salary.entity.OperrateLogs;
import com.boss.salary.entity.ReportTimeControl;
import com.boss.salary.service.IOperrateLogsService;
import com.boss.salary.service.IReportTimeControlService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;

@RestController
@RequestMapping(value = "/reportTimeControlController", produces = MediaType.APPLICATION_JSON)
public class ReportTimeControlController extends BasicinfoBaseController {


    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(ReportTimeControlController.class);

    @Autowired
    private IReportTimeControlService iReportTimeControlService;
    @Autowired
    private IOperrateLogsService iOperrateLogsService;

    /**
     * 查询
     * @return
     */
    @GetMapping("/getReportTimeControl")
    public ApiResultDTO getReportTimeControl(@RequestHeader("tokenid") String tokenid){
        try {
            String province = getUser(tokenid).getProvince();
            ReportTimeControl reportTimeControl = iReportTimeControlService.getReportTimeControl(province);
            return ApiResultDTO.success(reportTimeControl, "查询成功");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询失败--请先保存");
        }
    }

    /**
     * 修改
     * @return
     */
    @PostMapping("/updateReportTimeControl")
    public ApiResultDTO updateReportTimeControl(@RequestHeader("tokenid") String tokenid,
                                                @RequestBody ReportTimeControl reportTimeControl){
        try {
            UserDTO userDTO = getUser(tokenid);
            reportTimeControl.setMofDivCode(userDTO.getProvince());
            //添加操作日志
            OperrateLogs operrateLogs= new OperrateLogs();
            operrateLogs.setMofDivCode(userDTO.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDTO.getAgency());
            operrateLogs.setUeserName(userDTO.getName());
            operrateLogs.setOperateName("工资管理配置--上报时间控制--修改");
            iOperrateLogsService.saveOperrateLogs(operrateLogs);

            iReportTimeControlService.updateReportTimeControl(reportTimeControl);
            return ApiResultDTO.success(null, "修改成功");
        } catch (Exception e) {
            logger.error("修改 上报时间控制异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "修改失败");
        }
    }
}
